package com.max.common.oauth.oauth10.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.max.common.oauth.oauth10.OAuthApiBase;
import com.max.common.oauth.oauth10.OAuthParameter;
import com.max.common.oauth.oauth10.OAuthToken;
import com.max.common.util.HttpTool;
import com.max.common.util.HttpTool.Response;

/**curl -u "username:password" "http://api.t.sina.com.cn/statuses/user_timeline.xml?source=appkey&user_id=11051"*/
public class OAuthApiSinaMicroBlog extends OAuthApiBase
{


	public OAuthApiSinaMicroBlog() 
	{
		super("max_microblog_sina_01");
	}


	/**
	 * 
	 * @param method
	 * @param url
	 * @param bodyParamList
	 * @param token
	 * @return
	 */
	public Response request(String method,String url, List<OAuthParameter> paramList, OAuthToken token)
	{
		Response res = null;
		
		List<OAuthParameter> list = new ArrayList<OAuthParameter>();
		List<OAuthParameter> oauthParamList = generateOAuthParams(token);
		
		if(paramList == null)
			paramList = new ArrayList<OAuthParameter>();
		//统一增加source参数
		paramList.add(new OAuthParameter("source", consumer.getConsumer_key()));
		
		list.addAll(paramList);
		list.addAll(oauthParamList);
		
		String signature = signParameters(method,url,list,token);
		oauthParamList.add(new OAuthParameter("oauth_signature", signature));
		
		String authorization = "OAuth " + encodeParameters(oauthParamList, ",", true);
		
		Map<String,String> headParamMap = new HashMap<String,String>();
		headParamMap.put("Authorization", authorization);
		
		
		Map<String,String> bodyParamMap = new LinkedHashMap<String,String>();
		for(OAuthParameter param:paramList)
			bodyParamMap.put(param.getName(), param.getValue());
		
		HttpTool httpTool = new HttpTool();
		res = httpTool.request(method, url, headParamMap, bodyParamMap);
		
		return res;
	}
	
	
	public static void main(String[] args) throws Exception
	{
		OAuthApiSinaMicroBlog api = new OAuthApiSinaMicroBlog();
		OAuthToken token = api.requestToken();
//		System.out.println(token);
	}
}
